﻿Imports System.Drawing
Friend Class Caret
    Dim mhWnd As IntPtr
    Dim mWidth As Byte = 1
    Dim mHeight As Byte
    Dim mIME As Ime
    ''' <summary>
    ''' 光标的表示状态
    ''' </summary>
    ''' <remarks></remarks>
    Public IsCaretVisible As Boolean = False
    Sub New(ByVal hwnd As IntPtr, ByVal Height As Byte, ByVal inIME As Ime)
        mhWnd = hwnd
        mHeight = Height
        mIME = inIME
    End Sub
    Sub ChangeWidth(ByVal Width As Byte)
        If Not (mWidth = Width) Then
            mWidth = Width
            DestroyCaret()
            mdlWinAPI.CreateCaret(mhWnd, 0, mWidth, mHeight)
            ShowCaret()
        End If
    End Sub
    Sub ChangeHeight(ByVal Height As Byte)
        If Not (mHeight = Height) Then
            mHeight = Height
            DestroyCaret()
            Do While mdlWinAPI.CreateCaret(mhWnd, 0, mWidth, mHeight) = False
                MsgBox("ChangeHeight API ERROR")
            Loop
            ShowCaret()
        End If
    End Sub
    Public Sub CreateCaret()
        Do While mdlWinAPI.CreateCaret(mhWnd, 0, mWidth, mHeight) = False
            MsgBox("CreateCaret API ERROR")
        Loop
        IsCaretVisible = False
    End Sub
    Public Sub ShowCaret()
        If IsCaretVisible = False Then
            Do While mdlWinAPI.ShowCaret(mhWnd) = False
                MsgBox("ShowCaret API ERROR")
            Loop
            IsCaretVisible = True
        End If
    End Sub
    Public Sub HideCaret()
        If IsCaretVisible Then
            Do While mdlWinAPI.HideCaret(mhWnd) = False
                MsgBox("HideCaret API ERROR")
            Loop
            IsCaretVisible = False
        End If
    End Sub
    Public Sub DestroyCaret()
        mdlWinAPI.DestroyCaret()
        IsCaretVisible = False
    End Sub
    Public Sub SetCaretPos(ByVal Pos As Drawing.Point)
        mdlWinAPI.SetCaretPos(Pos.X, Pos.Y)
        mIME.SetIMEWindowLocation(Pos.X, Pos.Y)
    End Sub
End Class
